/**
 * @name 表单验证JS帮助类
 * @remark IsRequired 必输验证
 * @remark IsMaxLength
 * @remark 
 */
; var VerificationHelper = (function ($, window, document, undefined) {
	/**
	 * 返回content中输入框验证结果
	 * @param content div或from的ID
	 * @return 验证结果：true，验证通过；false，验证不通过
	 */
	var checkFrom = function(content){
		content = content || "body";
		content = content == "body" ? content : "#" + content;

		//去除所有错误提示
		$(".warn-box", content).remove();
		//验证
		$("input[type='text']", content).each(function (index, dom) {
			checkInput($(this));
		});
		$("input[type='password']", content).each(function (index, dom) {
			checkInput($(this));
		});
		$("select", content).each(function (index, dom) {
			checkSelect($(this));
		});
		$("textarea", content).each(function (index, dom) {
			checkTextarea($(this));
		});
		//若控件值发生变动，则去除错误提示
		$("input[type='text'],input[type='password'],textarea", content).unbind("change").change(function () {
			$(this).next(".warn-box").remove();
		});
		$("select", content).change(function () {
			$(this).next(".warn-box").remove();
		});

		//检测页面是否有错误提示，有则返回false
		if ($(".warn-box", content).size() > 0) {
			return false;
		} else {
			return true;
		}
	};
	/**
	 * 检测input输入
	 * @param textBox jquery对象
	 */
	var checkInput = function(textBox){
		//验证必填项
		if (textBox.hasClass("IsRequired") && ($.trim(textBox.val()) == "" || textBox.val() == undefined)) {
			showValidateMsg(textBox.attr("id"), "请输入" + textBox.attr("MsgName"));
			return;
		}
		//验证最大长度
		if (textBox.hasClass("IsMaxLength") && $.trim(textBox.val()).length > 0 && $.trim(textBox.val()).length > parseInt(textBox.attr("MaxLength"))) {
			showValidateMsg(textBox.attr("id"), textBox.attr("MsgName") + "的长度不能大于" + textBox.attr("MaxLength"));
			return;
		}
		//验证最小长度
		if (textBox.hasClass("IsMinLength") && $.trim(textBox.val()).length > 0 && $.trim(textBox.val()).length < parseInt(textBox.attr("MinLength"))) {
			showValidateMsg(textBox.attr("id"), textBox.attr("MsgName") + "的长度不能小于" + textBox.attr("MinLength"));
			return;
		}
		//验证最大数
		if (textBox.hasClass("IsMaxNumber") && parseInt(textBox.val(), 10) > parseInt(textBox.attr("MaxNumber"), 10)) {
			showValidateMsg(textBox.attr("id"), textBox.attr("MsgName") + "不能大于" + textBox.attr("MaxNumber"));
			return;
		}
		//验证最小数
		if (textBox.hasClass("IsMinNumber") && parseInt(textBox.val(), 10) < parseInt(textBox.attr("MinNumber"), 10)) {
			showValidateMsg(textBox.attr("id"), textBox.attr("MsgName") + "不能小于" + textBox.attr("MinNumber"));
			return;
		}
		//验证浮点数
		if (textBox.hasClass("IsFloat") && $.trim(textBox.val()).length > 0 && !/^\d+(\.\d+)?$/.test(textBox.val())) {
			showValidateMsg(textBox.attr("id"), textBox.attr("MsgName") + "必须为数字");
			return;
		}
		//保留一位小数
		if (textBox.hasClass("IsFloat1") && $.trim(textBox.val()).length > 0 && !/^\d+(\.\d)?$/.test(textBox.val())) {
			showValidateMsg(textBox.attr("id"), textBox.attr("MsgName") + "为最多一位小数的数字");
			return;
		}
		//验证最大浮点数
		if (textBox.hasClass("IsMaxFloat") && parseFloat(textBox.val()) > parseFloat(textBox.attr("MaxFloat"))) {
			showValidateMsg(textBox.attr("id"), textBox.attr("MsgName") + "不能大于" + textBox.attr("MaxFloat"));
			return;
		}
		//验证最小浮点数
		if (textBox.hasClass("IsMinFloat") && parseFloat(textBox.val()) < parseFloat(textBox.attr("MinFloat"))) {
			showValidateMsg(textBox.attr("id"), textBox.attr("MsgName") + "不能小于" + textBox.attr("MinFloat"));
			return;
		}
		//验证整数
		if (textBox.hasClass("IsNumber") && $.trim(textBox.val()).length > 0 && !/^\d+$/.test(textBox.val())) {
			showValidateMsg(textBox.attr("id"), textBox.attr("MsgName") + "必须为数字");
			return;
		}
		//验证正则表达式
		if (textBox.hasClass("IsReg") && $.trim(textBox.val()).length > 0 && !new RegExp("^" + textBox.attr("Reg") + "$").test($.trim(textBox.val()))) {
			showValidateMsg(textBox.attr("id"), textBox.attr("MsgReg"));
			return;
		}
		//验证数据唯一性
		if (textBox.hasClass("IsRemote")) {
			var romoteUrl = textBox.attr("RemoteUrl") ? textBox.attr("RemoteUrl") : "";
			var romoteId = textBox.attr("RemoteId") ? $("#" + textBox.attr("RemoteId")).val() : "0";
			var romoteKey = textBox.attr("RemoteKey") ? textBox.attr("RemoteKey") : "";
			var data = {};
			data["Id"] = romoteId;
			data[romoteKey] = $.trim(textBox.val());
			data["rid"] = Math.random();
			var validationResult = false;
			if (romoteUrl != "" && $.trim(textBox.val()).length > 0) {
				$.ajax({
					url: romoteUrl,
					dataType: "json",
					async: false, //验证设为同步检测
					data: data,
					type: "GET",
					headers: { 'GTA-Login-Token': $("#GTA-Login-Token").val() },
					success: function (data) {
						if (!$.checkAPI(data)) return;

						if (data) {
							validationResult = true;
							showValidateMsg(textBox.attr("id"),"该"+ textBox.attr("MsgName") + "已存在");
						}
					}
				});
			}
			if (validationResult)
				return;
		}
		//非法字符验证
		if ($.trim(textBox.val()).length > 0 && /((<[^>]+>)|(<[^>]+)|([^>]+>))/g.test(textBox.val())) {
			showValidateMsg(textBox.attr("id"), "请不要输入非法字符！");
			return;
		}
	};
	/**
	 * 检测select输入
	 * @param select jquery对象
	 */
	var checkSelect = function(select){
		//验证必填项
		if (select.hasClass("IsRequired") && (select.val() == "" || select.val() == undefined || select.val() == "0" || select.val() == "请选择")) {
			showValidateMsg(select.attr("id"), "请选择" + select.attr("MsgName"));
			return;
		}
	};
	/**
	 * 检测textarea输入
	 * @param textarea jquery对象
	 */
	var checkTextarea = function(textarea){
		//验证必填项
		if (textarea.hasClass("IsRequired") && ($.trim(textarea.val()) == "" || textarea.val() == undefined)) {
			showValidateMsg2(textarea.attr("id"),"请输入"+ textarea.attr("MsgName"));
			return;
		}
		//验证最大长度
		if (textarea.hasClass("IsMaxLength") && $.trim(textarea.val()).length > 0 && $.trim(textarea.val()).length > parseInt(textarea.attr("MaxLength"))) {
			showValidateMsg2(textarea.attr("id"), textarea.attr("MsgName") + "的长度不能大于" + textarea.attr("MaxLength") + "字");
			return;
		}
		//验证最小长度
		if (textarea.hasClass("IsMinLength") && $.trim(textarea.val()).length > 0 && $.trim(textarea.val()).length < parseInt(textarea.attr("MinLength"))) {
			showValidateMsg2(textarea.attr("id"), textarea.attr("MsgName") + "的长度不能小于" + textarea.attr("MinLength") + "字");
			return;
		}
		//非法字符验证
		if ($.trim(textarea.val()).length > 0 && /<\/?[^>]*>/g.test(textarea.val())) {
			showValidateMsg2(textarea.attr("id"), "请不要输入非法字符！");
			return;
		}
	};
	/**
	 * 错误提示1(input,select使用)
	 * @param ctrlName 需要提示的dom元素ID
	 * @param msg 提示信息
	 */
	var showValidateMsg = function(ctrlName, msg){
		$("#" + ctrlName).addClass("warn");
		var validMsg = "<div class=\"warn-box\" id=\"warn-" + ctrlName + "\" style=\"display: inline-block;\"><b style=\"display: none;\"></b><p style=\"display: none;\">" + msg + "</p></div>";
		$(validMsg).insertAfter($("#" + ctrlName));
		$("#warn-" + ctrlName).hover(function () {
			$("#warn-" + ctrlName + " > b").toggle();
			$("#warn-" + ctrlName + " > p").toggle();
		});
	};
	/**
	 * 错误提示2(textarea使用)
	 * @param ctrlName 需要提示的dom元素ID
	 * @param msg 提示信息
	 */
	var showValidateMsg2 = function(ctrlName, msg){
		$("#" + ctrlName).addClass("warn");
		var outerHeight = $("#" + ctrlName).outerHeight();
		var validMsg = "<div class=\"warn-box\" id=\"warn-" + ctrlName + "\" style=\"display: inline-block;z-index: 12;margin-top:-" + outerHeight + "px;\"><b style=\"display: none;\"></b><p style=\"display: none;\">" + msg + "</p></div>";
		$(validMsg).insertAfter($("#" + ctrlName));
		$("#warn-" + ctrlName).hover(function () {
			$("#warn-" + ctrlName + " > b").toggle();
			$("#warn-" + ctrlName + " > p").toggle();
		});
	};
	/**
	 * 设置表单内容是否只读
	 * @param content div或from的ID
	 * @param flag 设置表单只读：true，只读；false，可输入
	 */
	var ChangeStatus = function(content, flag){
		content = content || "body";
		content = content == "body" ? content : "#" + content;
		if (flag) {
			//设为只读
			$("input[type='text'],input[type='checkbox'],select,textarea", content).each(function (index, dom) {
				$(this).attr({ "disabled": "disabled" });
			});
		} else {
			//取消只读
			$("input[type='text'],input[type='checkbox'],select,textarea", content).each(function (index, dom) {
				$(this).removeAttr("disabled");
			});
		}
	};
	
	return {
		checkFrom: checkFrom,
		ChangeStatus: ChangeStatus,
	};
})(jQuery, window, document);